<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>conditionaldelay_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_async_conditionaldelay_test.js.html">conditionaldelay_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>goog.provide(&#39;goog.async.ConditionalDelayTest&#39;);
<a name="line15"></a>goog.setTestOnly(&#39;goog.async.ConditionalDelayTest&#39;);
<a name="line16"></a>
<a name="line17"></a>goog.require(&#39;goog.async.ConditionalDelay&#39;);
<a name="line18"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>
<a name="line21"></a>var invoked = false;
<a name="line22"></a>var delay = null;
<a name="line23"></a>var clock = null;
<a name="line24"></a>var returnValue = true;
<a name="line25"></a>var onSuccessCalled = false;
<a name="line26"></a>var onFailureCalled = false;
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>function callback() {
<a name="line30"></a>  invoked = true;
<a name="line31"></a>  return returnValue;
<a name="line32"></a>}
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>function setUp() {
<a name="line36"></a>  clock = new goog.testing.MockClock(true);
<a name="line37"></a>  invoked = false;
<a name="line38"></a>  returnValue = true;
<a name="line39"></a>  onSuccessCalled = false;
<a name="line40"></a>  onFailureCalled = false;
<a name="line41"></a>  delay = new goog.async.ConditionalDelay(callback);
<a name="line42"></a>  delay.onSuccess = function() {
<a name="line43"></a>    onSuccessCalled = true;
<a name="line44"></a>  };
<a name="line45"></a>  delay.onFailure = function() {
<a name="line46"></a>    onFailureCalled = true;
<a name="line47"></a>  };
<a name="line48"></a>}
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>function tearDown() {
<a name="line52"></a>  clock.dispose();
<a name="line53"></a>  delay.dispose();
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>function testDelay() {
<a name="line58"></a>  delay.start(200, 200);
<a name="line59"></a>  assertFalse(invoked);
<a name="line60"></a>
<a name="line61"></a>  clock.tick(100);
<a name="line62"></a>  assertFalse(invoked);
<a name="line63"></a>
<a name="line64"></a>  clock.tick(100);
<a name="line65"></a>  assertTrue(invoked);
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>
<a name="line69"></a>function testStop() {
<a name="line70"></a>  delay.start(200, 500);
<a name="line71"></a>  assertTrue(delay.isActive());
<a name="line72"></a>
<a name="line73"></a>  clock.tick(100);
<a name="line74"></a>  assertFalse(invoked);
<a name="line75"></a>
<a name="line76"></a>  delay.stop();
<a name="line77"></a>  clock.tick(100);
<a name="line78"></a>  assertFalse(invoked);
<a name="line79"></a>
<a name="line80"></a>  assertFalse(delay.isActive());
<a name="line81"></a>}
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>function testIsActive() {
<a name="line85"></a>  assertFalse(delay.isActive());
<a name="line86"></a>  delay.start(200, 200);
<a name="line87"></a>  assertTrue(delay.isActive());
<a name="line88"></a>  clock.tick(200);
<a name="line89"></a>  assertFalse(delay.isActive());
<a name="line90"></a>}
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>function testRestart() {
<a name="line94"></a>  delay.start(200, 50000);
<a name="line95"></a>  clock.tick(100);
<a name="line96"></a>
<a name="line97"></a>  delay.stop();
<a name="line98"></a>  assertFalse(invoked);
<a name="line99"></a>
<a name="line100"></a>  delay.start(200, 50000);
<a name="line101"></a>  clock.tick(199);
<a name="line102"></a>  assertFalse(invoked);
<a name="line103"></a>
<a name="line104"></a>  clock.tick(1);
<a name="line105"></a>  assertTrue(invoked);
<a name="line106"></a>
<a name="line107"></a>  invoked = false;
<a name="line108"></a>  delay.start(200, 200);
<a name="line109"></a>  clock.tick(200);
<a name="line110"></a>  assertTrue(invoked);
<a name="line111"></a>
<a name="line112"></a>  assertFalse(delay.isActive());
<a name="line113"></a>}
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>function testDispose() {
<a name="line117"></a>  delay.start(200, 200);
<a name="line118"></a>  delay.dispose();
<a name="line119"></a>  assertTrue(delay.isDisposed());
<a name="line120"></a>
<a name="line121"></a>  clock.tick(500);
<a name="line122"></a>  assertFalse(invoked);
<a name="line123"></a>}
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>function testConditionalDelay_Success() {
<a name="line127"></a>  returnValue = false;
<a name="line128"></a>  delay.start(100, 300);
<a name="line129"></a>
<a name="line130"></a>  clock.tick(99);
<a name="line131"></a>  assertFalse(invoked);
<a name="line132"></a>  clock.tick(1);
<a name="line133"></a>  assertTrue(invoked);
<a name="line134"></a>
<a name="line135"></a>  assertTrue(delay.isActive());
<a name="line136"></a>  assertFalse(delay.isDone());
<a name="line137"></a>  assertFalse(onSuccessCalled);
<a name="line138"></a>  assertFalse(onFailureCalled);
<a name="line139"></a>
<a name="line140"></a>  returnValue = true;
<a name="line141"></a>
<a name="line142"></a>  invoked = false;
<a name="line143"></a>  clock.tick(100);
<a name="line144"></a>  assertTrue(invoked);
<a name="line145"></a>
<a name="line146"></a>  assertFalse(delay.isActive());
<a name="line147"></a>  assertTrue(delay.isDone());
<a name="line148"></a>  assertTrue(onSuccessCalled);
<a name="line149"></a>  assertFalse(onFailureCalled);
<a name="line150"></a>
<a name="line151"></a>  invoked = false;
<a name="line152"></a>  clock.tick(200);
<a name="line153"></a>  assertFalse(invoked);
<a name="line154"></a>}
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>function testConditionalDelay_Failure() {
<a name="line158"></a>  returnValue = false;
<a name="line159"></a>  delay.start(100, 300);
<a name="line160"></a>
<a name="line161"></a>  clock.tick(99);
<a name="line162"></a>  assertFalse(invoked);
<a name="line163"></a>  clock.tick(1);
<a name="line164"></a>  assertTrue(invoked);
<a name="line165"></a>
<a name="line166"></a>  assertTrue(delay.isActive());
<a name="line167"></a>  assertFalse(delay.isDone());
<a name="line168"></a>  assertFalse(onSuccessCalled);
<a name="line169"></a>  assertFalse(onFailureCalled);
<a name="line170"></a>
<a name="line171"></a>  invoked = false;
<a name="line172"></a>  clock.tick(100);
<a name="line173"></a>  assertTrue(invoked);
<a name="line174"></a>  assertFalse(onSuccessCalled);
<a name="line175"></a>  assertFalse(onFailureCalled);
<a name="line176"></a>
<a name="line177"></a>  invoked = false;
<a name="line178"></a>  clock.tick(90);
<a name="line179"></a>  assertFalse(invoked);
<a name="line180"></a>  clock.tick(10);
<a name="line181"></a>  assertTrue(invoked);
<a name="line182"></a>
<a name="line183"></a>  assertFalse(delay.isActive());
<a name="line184"></a>  assertFalse(delay.isDone());
<a name="line185"></a>  assertFalse(onSuccessCalled);
<a name="line186"></a>  assertTrue(onFailureCalled);
<a name="line187"></a>}
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>function testInfiniteDelay() {
<a name="line191"></a>  returnValue = false;
<a name="line192"></a>  delay.start(100, -1);
<a name="line193"></a>
<a name="line194"></a>  // Test in a big enough loop.
<a name="line195"></a>  for (var i = 0; i &lt; 1000; ++i) {
<a name="line196"></a>    clock.tick(80);
<a name="line197"></a>    assertTrue(delay.isActive());
<a name="line198"></a>    assertFalse(delay.isDone());
<a name="line199"></a>    assertFalse(onSuccessCalled);
<a name="line200"></a>    assertFalse(onFailureCalled);
<a name="line201"></a>  }
<a name="line202"></a>
<a name="line203"></a>  delay.stop();
<a name="line204"></a>  assertFalse(delay.isActive());
<a name="line205"></a>  assertFalse(delay.isDone());
<a name="line206"></a>  assertFalse(onSuccessCalled);
<a name="line207"></a>  assertFalse(onFailureCalled);
<a name="line208"></a>}
<a name="line209"></a>
<a name="line210"></a>function testCallbackScope() {
<a name="line211"></a>  var callbackCalled = false;
<a name="line212"></a>  var scopeObject = {};
<a name="line213"></a>  function internalCallback() {
<a name="line214"></a>    assertEquals(this, scopeObject);
<a name="line215"></a>    callbackCalled = true;
<a name="line216"></a>    return true;
<a name="line217"></a>  }
<a name="line218"></a>  delay = new goog.async.ConditionalDelay(internalCallback, scopeObject);
<a name="line219"></a>  delay.start(200, 200);
<a name="line220"></a>  clock.tick(201);
<a name="line221"></a>  assertTrue(callbackCalled);
<a name="line222"></a>}
</pre>


</body>
</html>
